﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.Models;
using Microhard.WeixinMP.DTO;
using Microhard.Core;

namespace Microhard.WeixinMP.Impls
{
    public class WXMPAccountImpl : IWXMPAccount
    {
        public Core.DBUtility.ExecutedResult Add(Models.WXMPAccountModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            MPAccount op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.UpdateTime = DateTime.Now;

            try
            {
                db.MPAccounts.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("WXMPAccountImpl.Add", "微信平台账户添加失败");
                Log.Error("WXMPAccountImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.WXMPAccountModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPAccounts.SingleOrDefault(c => c.WXMPID == model.WXMPID);

            if (entity != null)
            {
                entity.UserName = model.UserName;
                entity.Password = model.Password;
                entity.AppID = model.AppID;
                entity.AppSecret = model.AppSecret;
                entity.CompanyID = model.CompanyID;
                entity.CompanyName = model.CompanyName;
                entity.Welcome = model.Welcome;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("Resource.Update", "修改微信平台账户失败");
                Log.Error("WXMPAccountImpl.Update", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var toDeltes = from b in db.MPAccounts
                           where ids.Contains(b.WXMPID) && b.MPUsers.Count == 0
                           select b;
            foreach (var d in toDeltes)
            {
                db.MPAccounts.DeleteObject(d);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("WXMPAccountImpl.Delete", "微信平台账户删除失败");
                Log.Error("WXMPAccountImpl.Delete", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.WXMPAccountModel model)
        {
            throw new NotImplementedException();
        }

        public Models.WXMPAccountModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPAccounts.SingleOrDefault(m => m.WXMPID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Models.WXMPAccountModel GetModelByModel(Models.WXMPAccountModel model)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPAccounts.SingleOrDefault(m => m.UserName == model.UserName);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public List<Models.WXMPAccountModel> Query(Models.WXMPAccountModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            IEnumerable<MPAccount> entities = from b in db.MPAccounts
                                              select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<WXMPAccountModel>();
        }

        public List<Models.WXMPAccountModel> Query(Models.WXMPAccountModel Model)
        {
            if (Model == null)
            {
                MicrohardWXEntities db = new MicrohardWXEntities();
                IEnumerable<MPAccount> entities = from b in db.MPAccounts
                                                  select b;
                if (entities != null && entities.Count() > 0)
                    return entities.Select(m => m.ToModel()).ToList();
            }
            return new List<WXMPAccountModel>();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public WXMPAccountModel GetModelByCompanyID(Guid companyID)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPAccounts.SingleOrDefault(m => m.CompanyID == companyID);
            if (entity != null)
                return entity.ToModel();
            return null;
        }
    }
}
